


//
//  NProblemContentViewController.m
//  nncloud
//
//  Created by 朵 on 2018/9/23.
//  Copyright © 2018年 youedata. All rights reserved.
//
#import "UITextView+PlaceHolder.h"
#import "NProblemContentViewController.h"
#import "NProblemContentCell.h"
#import "NProblemContentHeaderView.h"
#import "NLeaveWordBtnView.h"
#import "NWriteMsgViewController.h"
#import "NProblemCellFrame.h"
#import "NProblemTableViewTitleViewCell.h"
#import "NProblemTableViewContentCell.h"
#import "NProblemTableViewQuestionCell.h"
#import "NProblemTableViewImagesCell.h"
#import "NProblemTableViewAddContentCell.h"
#import "NCommitFBViewController.h"
#import "NFeedbackPopView.h"
#import "NWriteMsgViewController.h"
#import "ZFActionSheet.h"
#import "NConfirmPopView.h"
#import "NStarPopView.h"
#import "STPickerSingle.h"
#import "NUnitRoomModel.h"

@interface NProblemContentViewController ()<UITableViewDelegate,UITableViewDataSource,ZFActionSheetDelegate,STPickerSingleDelegate>

@property (nonatomic,strong)UITableView *mainTableView;
@property (nonatomic,strong)NProblemContentHeaderView *headerView;
@property (nonatomic,strong)NLeaveWordBtnView *leaveBtn;
//@property (nonatomic,strong)NSArray *testArray;

@property (nonatomic,strong)NSMutableArray *mainDataSource;

@property (nonatomic,strong)NProblemModel *mainModel;

@property (nonatomic,strong) UIButton *rightNavigationBtn;

//收藏按钮
@property (nonatomic,strong) UIButton *collcetionBtn;

@property (nonatomic,strong) NSArray *actionSheetSource;

@property (nonatomic,strong) NLeaveWordBtnView *bottomBtn;

@property (nonatomic,strong) NLeaveWordBtnView *bottomLeftBtn;

@property (nonatomic,strong) NLeaveWordBtnView *bottomRightBtn;

//@property (nonatomic,strong) UIView *bottomView;

//@property (nonatomic,strong)NFeedbackPopView *pop;
//部门数组数据
@property (nonatomic, strong) NSArray *departmentSource;
//收藏
@property (nonatomic, assign) BOOL isCollection;
//权限判断
@property (nonatomic,copy)NSString *questionAuth;


//问题流程的步骤数
@property (nonatomic,assign) NSInteger questionProgress;

@property (nonatomic,copy)NSString *officeName;

@property (nonatomic,strong)NStarPopView *popView;

///标记已经出现过星星

@property (nonatomic,assign)BOOL hasShowStar;

@end
#define cellIdentifier @"contentid"
#define footerBtnHeight  50
#define ActionSheetTextClose    @"关闭该反馈"
#define ActionSheetTextDelete   @"删除该反馈"
#define ActionSheetTextTransfer @"转交相关部门"
#define ActionSheetTextReply    @"自行答复该问题"
@implementation NProblemContentViewController
-(UITableView *)mainTableView{
    
    if (!_mainTableView) {
        
        _mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0 , 0, ScreenWidth , ScreenHeight - 64 ) style:UITableViewStyleGrouped];
        _mainTableView.delegate = self;
        _mainTableView.dataSource = self;
        _mainTableView.rowHeight = 136.f;
        _mainTableView.backgroundColor = kBackgroundColor;
        _mainTableView.separatorStyle = UITableViewCellSelectionStyleNone;
        //        [_mainTableView registerNib:[UINib nibWithNibName:@"NProblemContentCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
        _mainTableView.showsVerticalScrollIndicator = NO;
        _mainTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, footerBtnHeight)];
    }
    return _mainTableView;
}
//底部留言按钮 1个时候
- (NLeaveWordBtnView *)bottomBtn{
    
    if (!_bottomBtn) {
        _bottomBtn = [[NLeaveWordBtnView alloc] initWithFrame:CGRectMake(10, ScreenHeight - 64 - footerBtnHeight, ScreenWidth - 20, footerBtnHeight - 10)];
        _bottomBtn.backgroundColor = NColorFrom16RGB(0x75ccf8);
        //点击按钮 回调
        //        _bottomBtn.didSelectBtn = ^(){
        //
        //        };
    }
    return _bottomBtn;
}
//底部留言按钮 2个   左边
- (NLeaveWordBtnView *)bottomLeftBtn{
    //底部留言按钮
    if (!_bottomLeftBtn) {
        _bottomLeftBtn = [[NLeaveWordBtnView alloc] initWithFrame:CGRectMake(10, ScreenHeight - 64 - footerBtnHeight, (ScreenWidth - 20) / 2 - 10, footerBtnHeight - 10)];
        _bottomLeftBtn.backgroundColor = NColorFrom16RGB(0x75ccf8);
        _bottomLeftBtn.layer.cornerRadius = 8.0;
        _bottomLeftBtn.layer.masksToBounds = YES;
        [_bottomLeftBtn setBtnText:@"bbb"];
        //点击按钮 回调
        //        _bottomLeftBtn.didSelectBtn = ^(){
        //
        //        };
    }
    return _bottomLeftBtn;
}
//底部留言按钮 2个    右边
- (NLeaveWordBtnView *)bottomRightBtn{
    //底部留言按钮
    if (!_bottomRightBtn) {
        _bottomRightBtn = [[NLeaveWordBtnView alloc] initWithFrame:CGRectMake((ScreenWidth - 20) / 2 + 20, ScreenHeight - 64 - footerBtnHeight, (ScreenWidth - 20) / 2 - 10, footerBtnHeight - 10)];
        _bottomRightBtn.backgroundColor = NColorFrom16RGB(0x75ccf8);
        _bottomRightBtn.layer.cornerRadius = 8.0;
        _bottomRightBtn.layer.masksToBounds = YES;
        [_bottomRightBtn setBtnText:@"aaa"];
        //点击按钮 回调
        //        _bottomRightBtn.didSelectBtn = ^(){
        //
        //        };
    }
    return _bottomRightBtn;
}
///右上角
- (UIButton *)rightNavigationBtn{
    if (!_rightNavigationBtn) {
        _rightNavigationBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _rightNavigationBtn.frame = CGRectMake(0, 0, 40, 30);
        [_rightNavigationBtn setTitleColor:NColorFrom16RGB(0x75ccf8) forState:UIControlStateNormal];
        [_rightNavigationBtn setTitle:nil forState:UIControlStateNormal];
        [_rightNavigationBtn addTarget:self action:@selector(clickRightBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightNavigationBtn;
}

//导航栏 右面按钮
- (void)creatRightBtn
{
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.rightNavigationBtn];
}


- (void)viewDidAppear:(BOOL)animated
{
//    [self request];
//    [super viewDidAppear:animated];
    //收藏按钮  这个在数据请求之后进行
//    [SVProgressHUD dismiss];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self createCollectionBtn];
        
    });
}
- (void)viewWillAppear:(BOOL)animated{
    //添加反馈后返回刷新页面
    if ([NUserModel shareInstance].isRefreshContentvView == YES) {
        [self loadDetialData];
        [NUserModel shareInstance].isRefreshContentvView = NO;
    }
}
- (void)viewDidLoad {
    //反馈详情
    [super viewDidLoad];
    self.view.backgroundColor = kWhiteColor;
    [self.navigationItem setItemWithTitle:@"问题内容" textColor:kNavigationTitleColor fontSize:18 itemType:center];
    [self.view addSubview:self.mainTableView];
    
    //添加bool标记，修复星星评价弹出两次的问题
    self.hasShowStar = NO;
    
    self.questionProgress = 0;
    //action 数组
    self.actionSheetSource = [NSArray array];
//  创建导航栏右上角按钮
    [self creatRightBtn];
    //请求内容数据
    [self loadDetialData];

}
#pragma mark - //创建收藏按钮
- (void)createCollectionBtn {
    
    if (self.pushType == NProblemPushTypeCollection) {
        //登录状态
        if ([NUserModel shareInstance].isLogin) {
            //且后台返回是收藏状态
            if (self.isCollection==YES) {
                
                self.rightNavigationBtn.hidden = false;
                 [self.rightNavigationBtn setImage:[UIImage imageNamed:@"qes_collect_yes"] forState:UIControlStateNormal];
                
                
            }else{
                self.rightNavigationBtn.hidden = false;
                [self.rightNavigationBtn setImage:[UIImage imageNamed:@"qes_collect_no"] forState:UIControlStateNormal];
            }
            
        }else{
            
            self.rightNavigationBtn.hidden = false;
            [self.rightNavigationBtn setImage:[UIImage imageNamed:@"qes_collect_no"] forState:UIControlStateNormal];
            
            YYLog(@"打印还是useful的%@",self.rightNavigationBtn);
            
        }
        
        [self.rightNavigationBtn addTarget:self action:@selector(collectionRequest) forControlEvents:UIControlEventTouchUpInside];
    }
    
}
//收藏取消操作
- (void)collectionRequest {
    
    [self reqCollection];
}

#pragma mark - req 详情接口
- (void)loadDetialData
{
    //公开问题(登陆后)详情需要传userId，公开问题不登陆和普通问题详情则不需要
    NSString *userId = [NUserModel shareInstance].userInfoId;
//    [SVProgressHUD show];
    NSDictionary *dic = @{@"questionId":_problemID,@"userId":userId?userId:@""};
    [[NNRCNetRequest manager]getWithUrl:kProblemContent params:dic success:^(id result) {
        YYLog(@"%@",result);
        if ([[result objectForKey:@"code"] isEqualToString:@"0"]) {
            
            [self formattingDataSource:[result objectForKey:@"page"]];
            
        }else{
            [AFToast showText:@"获取详情失败"];
//            [SVProgressHUD dismiss];
        }
    } failure:^(id failure) {
        [AFToast showText:@"获取详情失败"];
//        [SVProgressHUD dismiss];
    }];
}
#pragma mark - 收藏req
- (void)reqCollection
{
    NSString *userId = [NUserModel shareInstance].userInfoId;
    if (!userId) {
        //登录后才可以收藏
        [self callLoginVC];
        return;
    }
    NSDictionary *dic = @{@"userId":userId,
                          @"questionId":self.mainModel.questionId,
                          };
    [[NNRCNetRequest manager]postWithUrl:kColletAdd params:dic success:^(id result) {
        YYLog(@"%@",result);
        if ([[result objectForKey:@"code"] isEqualToString:@"0"]) {
            [AFToast showText:@"操作成功"];
            self.isCollection = !self.isCollection;
            [self updateCollection];
        }else{
            [AFToast showText:@"操作失败"];
        }
    } failure:^(id failure) {
        YYLog(@"%@",failure);
         [AFToast showText:@"操作失败"];
    }];
}
- (void)updateCollection
{
    if (self.isCollection) {
        
        [self.rightNavigationBtn setImage:[UIImage imageNamed:@"qes_collect_yes"] forState:UIControlStateNormal];
    }else{
        
        [self.rightNavigationBtn setImage:[UIImage imageNamed:@"qes_collect_no"] forState:UIControlStateNormal];
    }
}
#pragma mark - 格式化数据
- (void)formattingDataSource:(NSDictionary *)dict
{
    self.mainDataSource = [NSMutableArray array];
    NProblemModel *model = [NProblemModel mj_objectWithKeyValues:dict];
    //权限判断 是否能操作
    self.questionAuth = model.questionAuth;
    //政府人员需要权限判断
    NSInteger userType = [[NUserModel shareInstance].userInfoType integerValue];
    if (userType==2) {
        if ([model.questionAuth integerValue]==0) {
            self.rightNavigationBtn.hidden = YES;
        }else{
            self.rightNavigationBtn.hidden = false;
        }
    }
    if ([model.questionCollet isEqualToString:@"1"]) {
        
        self.isCollection = YES;
        
    }else{
        
        self.isCollection = false;
        
    }
    
    self.mainModel = [NProblemModel mj_objectWithKeyValues:dict];
    //根据问题类型 创建 底部按钮
    [self updateBottomBtnAndNaviBtnWithQuestionProgress:model.questionProgress];
    
    if (self.pushType == NProblemPushTypeCollection) {
        //        [self updateCollection];
    }
    
    NProblemCellFrame *cellFrame = [[NProblemCellFrame alloc] init];
    model.cellType = ProblemCellTypeTTitle;
    cellFrame.model = model;
    [self.mainDataSource addObject:cellFrame];  //title
    
    NProblemCellFrame *cellFrame1 = [[NProblemCellFrame alloc] init];
    NProblemModel *model1 = [NProblemModel mj_objectWithKeyValues:dict];
    model1.cellType = ProblemCellTypeTContent;
    cellFrame1.model = model1;
    [self.mainDataSource addObject:cellFrame1];  //content
    
    for (int i = 0; i < [[dict objectForKey:@"childList"] count]; i++) {
        NProblemCellFrame *cellFrame = [[NProblemCellFrame alloc] init];
        NProblemModel *model = [NProblemModel mj_objectWithKeyValues:[[dict objectForKey:@"childList"] objectAtIndex:i]];
        model.cellType = ProblemCellTypeNormal; //问答
        cellFrame.model = model;
        if ([model.attachmentList count] > 0) {
            model.cellType = ProblemCellTypeImages;
            cellFrame.model = model;
        }
        if ([model.questionStatus integerValue] == 0) {
            model.cellType = ProblemCellTypeImages;
            cellFrame.model = model;
        }
        if ([model.questionProgress isEqualToString:@"3"]) {
            if ([[NUserModel shareInstance].userInfoType integerValue] == 1) {
                model.cellType = ProblemCellTypeNormal;
                cellFrame.model = model;
            }else{
                model.cellType = ProblemCellTypeAddContent;
                cellFrame.model = model;
            }
        }
        //        if (i == 3) {
        ////            NProblemTableViewAddContentCell
        //            model.cellType = ProblemCellTypeAddContent;
        //            cellFrame.model = model;
        //        }
        [self.mainDataSource addObject:cellFrame];
    }
    [self.mainTableView reloadData];
}
- (void)addView:(UIView *)view
{
    bool isHave = NO;
    for (UIView *tView in [self.view subviews]) {
        if (tView == view) {
            isHave = YES;
        }
    }
    if (!isHave) {
        [self.view addSubview:view];
    }
}
- (void)hiddenBottomBtn:(BOOL)hidden
{
    self.bottomLeftBtn.hidden = !hidden;
    self.bottomRightBtn.hidden = !hidden;
    self.bottomBtn.hidden = hidden;
}

#pragma mark -问题进度：//0待审核,1等待受理，2已受理，3已转交相关部门，4自行解答，5等待用户确认,6等待用户评价,7已评价,8已关闭,9审核未通过,10删除

#pragma mark - 根据问题类型和userType创建底部按钮  和 右上角按钮
- (void)rightBarbtnBusinessWithQuestionProgress:(NSString *)progress
{
    // ActionSheetTextClose , ActionSheetTextDelete  , ActionSheetTextTransfer , ActionSheetTextReply
    
    if (self.pushType == NProblemPushTypeCollection) {
        
//                [self reqCollection];
        
    }else{
        
        NSInteger userType = [[NUserModel shareInstance].userInfoType integerValue];
        WeakStament(ws);
        //0待审核,1等待受理，2已受理，3已转交相关部门，4自行解答，5等待用户确认,6等待用户评价,7已评价,8已关闭,9审核未通过,10删除
        ///个人端和企业端是一样的
        if (userType == 1||userType==0) {
            
            switch ([progress integerValue]) {
                case 0:
                case 1:
                case 9:
                {
//                    [ws closeFeedBack];
                    //关闭反馈 1031 添加  企业端关闭反馈后
                    [self updateProgressWithProgress:@"8" withErrorMsg:nil success:^(id aOjec) {
                        
                    } fail:^(NSError *aError) {
                        
                    }];
                }
                    break;
                case 2:
                case 3:
                case 4:
                case 5:
                {
                    [ws confirmAnswer];
                }
                case 8:
                {
                    
                }
                    break;
                default:
                    break;
            }
        }else{
            
            ///政府端
            //0待审核,1等待受理，2已受理，3已转交相关部门，4自行解答，5等待用户确认,6等待用户评价,7已评价,8已关闭,9审核未通过,10删除
            switch ([progress integerValue]) {
                case 0:
                case 1:
                case 6:
                case 7:
                case 9:
                {
                    ws.actionSheetSource = @[ActionSheetTextClose,ActionSheetTextDelete];
                    [ws feedBack];
                }
                    break;
                
                case 8://关闭问题
                {
                    ws.actionSheetSource = @[ActionSheetTextDelete];
                    [ws feedBack];
                }
                    break;
                case 2:
                case 3:
                case 4:
                case 5:
                case 10:
                {
                    ws.actionSheetSource = @[ActionSheetTextTransfer,ActionSheetTextReply,ActionSheetTextClose,ActionSheetTextDelete];
                    [ws feedBack];
                }
                default:
                    break;
            }
        }
    }
}

#pragma mrak - 根据权限判断处理按钮是否隐藏
- (void)updateBottomBtnAndNaviBtnWithQuestionProgress:(NSString *)progress
{
    if (self.pushType == NProblemPushTypeCollection) {
        
        [self hiddenAllBottomBtn];
        
    }else{
        //  * 0待审核,1等待受理，2已受理，3已转交相关部门，4自行解答，5等待用户确认,6等待用户评价,7已评价,8已关闭,9审核未通过，10删除
        //1 企业   2 政府
        NSInteger userType = [[NUserModel shareInstance].userInfoType integerValue];
        
        /////权限判断  只有政府端才有权限这个概念
        
        if (userType==2) {
            if ([self.questionAuth integerValue]==0) {
                self.rightNavigationBtn.hidden = YES;
            }else{
                self.rightNavigationBtn.hidden = false;
            }
        }
        //设置右上角按钮显示
        [self.rightNavigationBtn setTitle:((userType == 1)||(userType==0) ? @"关闭反馈" : @"处理反馈") forState:UIControlStateNormal];
        //根据不同的问题类型的进度，做对应的相关处理
        WeakStament(ws);
        switch ([progress integerValue]) {
            case 0:
            {
                if (userType == 1||userType==0) {
                    [self.bottomBtn setBtnText:@"继续留言"];
                    [self addView:self.bottomBtn];
                    self.bottomBtn.didSelectBtn = ^(){
                        //继续留言
                        [ws continueMessage];
                    };
                    [self hiddenBottomBtn:NO];
                    [self addView:self.bottomBtn];
                }else{
                    [self.bottomLeftBtn setBtnText:@"审核通过"];
                    self.bottomLeftBtn.didSelectBtn = ^(){
                        //审核通过
                        [ws approved];
                    };
                    [self.bottomRightBtn setBtnText:@"审核不通过"];
                    self.bottomRightBtn.didSelectBtn = ^(){
                        //审核不通过
                        [ws closeFeedBack];
                    };
                    [self addView:self.bottomLeftBtn];
                    [self addView:self.bottomRightBtn];
                    [self hiddenBottomBtn:YES];
                    
                }
                [self.rightNavigationBtn setTitle:((userType == 1)||(userType==0) ? @"关闭反馈" : @"处理反馈") forState:UIControlStateNormal];
            }
                break;
            case 1:
            {
                if (userType == 1||userType==0) {
                    [self hiddenBottomBtn:NO];
                    [self.bottomBtn setBtnText:@"继续留言"];
                    self.bottomBtn.didSelectBtn = ^(){
                        //继续留言
                        [ws continueMessage];
                    };
                }else{
                    WeakStament(ws);
                    [self hiddenBottomBtn:NO];
                    [self.bottomBtn setBtnText:@"受理该问题"];
                    self.bottomBtn.didSelectBtn = ^(){
                        //受理该问题
                        [ws acceptProblem];
                    };
                }
                [self addView:self.bottomBtn];
            }
                break;
            case 2:
            case 3:
            case 4:
            case 5:
            {
                if (userType == 1||userType==0) {
                    self.rightNavigationBtn.hidden = false;
                    [self hiddenBottomBtn:NO];
                    [self.bottomBtn setBtnText:@"继续留言"];
                    self.bottomBtn.didSelectBtn = ^(){
                        //继续留言
                        [ws continueMessage];
                    };
                }else{
                    //根据权限判断留言按钮是否显示
                    if ([self.questionAuth integerValue]==0) {
                        [self hiddenBottomBtn:YES];
                    }else{
                        [self hiddenBottomBtn:NO];
                    }
                    [self.bottomBtn setBtnText:@"留言"];
                    self.bottomBtn.didSelectBtn = ^(){
                        //留言
                        [ws continueMessage];
                    };
                }
                [self addView:self.bottomBtn];
                [self.rightNavigationBtn setTitle:((userType == 1)||(userType==0) ? @"确认解答" : @"处理反馈") forState:UIControlStateNormal];
            }
                break;
            case 6:
            {
                [self hiddenAllBottomBtn];
                
                if (userType == 1||userType==0) {
                    
                    self.rightNavigationBtn.hidden = YES;
                    //去评价
                    [self toStarSorce];
                    
                }else{
                    
                    ///政府端
                    //权限判断
                    if ([self.questionAuth integerValue]==0) {
                        self.rightNavigationBtn.hidden = YES;
                    }else{
                        self.rightNavigationBtn.hidden = NO;
                    }
                    [self.rightNavigationBtn setTitle:@"处理反馈" forState:UIControlStateNormal];
                }
                
            }
                break;
            case 7:
            case 8:
            {
                [self hiddenAllBottomBtn];
                
                if (userType == 1||userType==0) {
                    
                    self.rightNavigationBtn.hidden = YES;
                    
                }else{
                    //权限判断
                    if ([self.questionAuth integerValue]==0) {
                        self.rightNavigationBtn.hidden = YES;
                    }else{
                        self.rightNavigationBtn.hidden = NO;
                    }
                    [self.rightNavigationBtn setTitle:@"处理反馈" forState:UIControlStateNormal];
                }
            }
                break;
            case 10:
            {
                //全部按隐藏
                [self hiddenAllBtn];
            }
                break;
            case 9:
            {
                if (userType == 1||userType==0) {
                    [self hiddenBottomBtn:NO];
                    [self.bottomBtn setBtnText:@"重新发起问题反馈"];
                    self.bottomBtn.didSelectBtn = ^(){
                        //重新发起问题反馈
                        [ws againFeedback];
                    };
                    [self addView:self.bottomBtn];
                }else{
                    [self hiddenAllBottomBtn];
                }
                
                [self.rightNavigationBtn setTitle:((userType == 1)||(userType==0) ? @"关闭反馈" : @"处理反馈") forState:UIControlStateNormal];
            }
                break;
            default:
                break;
        }
    }
}
//隐藏底部所有按钮
- (void)hiddenAllBottomBtn
{
    [self hiddenBottomBtn:YES];
    self.bottomLeftBtn.hidden = YES;
    self.bottomRightBtn.hidden = YES;
}
//隐藏所有按钮
- (void)hiddenAllBtn
{
    [self hiddenBottomBtn:YES];
    self.bottomLeftBtn.hidden = YES;
    self.bottomRightBtn.hidden = YES;
    self.rightNavigationBtn.hidden = YES;
}

#pragma mark - delegate && datasource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NProblemCellFrame *cellFrame = [self.mainDataSource objectAtIndex:indexPath.section];
    return cellFrame.cellHeight;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.mainDataSource count];
}
//section头部间距
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01f;//section头部高度
}
//section头部视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1)];
    view.backgroundColor = [UIColor clearColor];
    return view;
}
//section底部间距
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    NProblemCellFrame *cellFrame = [self.mainDataSource objectAtIndex:section];
    if ([cellFrame.model.cellType isEqualToString:ProblemCellTypeTTitle] ||
        [cellFrame.model.cellType isEqualToString:ProblemCellTypeTContent]
        ) {
        return 10.0;
    }else{
        return 0.01;
    }
}
//section底部视图
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1)];
    view.backgroundColor = [UIColor clearColor];
    return view;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NProblemCellFrame *cellFrame = [self.mainDataSource objectAtIndex:indexPath.section];
    
    ///标题cell
    if ([cellFrame.model.cellType isEqualToString:ProblemCellTypeTTitle]) {
        NSString *cellID= @"NProblemTableViewTitleViewCell";
        NProblemTableViewTitleViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell = [[NProblemTableViewTitleViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        cell.model = cellFrame.model;
        return cell;
    }
    else if ([cellFrame.model.cellType isEqualToString:ProblemCellTypeTContent]){
        NSString *cellID= @"NProblemTableViewContentCell";
        NProblemTableViewContentCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell = [[NProblemTableViewContentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        cell.model = cellFrame.model;
        return cell;
    }
    else if ([cellFrame.model.cellType isEqualToString:ProblemCellTypeNormal]){
        NSString *cellID= @"NProblemTableViewQuestionCell";
        NProblemTableViewQuestionCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell = [[NProblemTableViewQuestionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        cell.model = cellFrame.model;
        return cell;
    }
    else if ([cellFrame.model.cellType isEqualToString:ProblemCellTypeImages]){
        NSString *cellID= @"NProblemTableViewImagesCell";
        NProblemTableViewImagesCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell = [[NProblemTableViewImagesCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        cell.model = cellFrame.model;
        return cell;
    }
    else if ([cellFrame.model.cellType isEqualToString:ProblemCellTypeAddContent]){
        
        //添加答复内容
        NSString *cellID= @"NProblemTableViewAddContentCell";
        NProblemTableViewAddContentCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell = [[NProblemTableViewAddContentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        
        cell.model = cellFrame.model;

        //根据处理流程步骤判断当前部门
        [self currentQuestionProgressWithModel:cell.model];
        //不是当前认领人无法进行相关操作，只能看
        if ([self.questionAuth integerValue]==1) {
            cell.problemBtn.hidden = false;
        }else{
            cell.problemBtn.hidden = YES;
        }
        WeakStament(ws);
        cell.didSelectAddProblem = ^(NProblemModel *model){
//            [ws continueMessage];
            //添加答复内容 和后台沟通后算是 自动回复
            [ws toRecover];
        };
        return cell;
    }
    //
    else{
        NSString *cellID= @"caaell";
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        return cell;
    }
}
#pragma mark ---- ///根据问题流程来获取到当前部门的值
- (void)currentQuestionProgressWithModel:(NProblemModel *)model{
    NSInteger currentProgress = [model.questionProgress integerValue];
    self.questionProgress = currentProgress;
    if (currentProgress>3||currentProgress==3) {
        self.officeName = model.questionOfficeName;
    }
}
#pragma mark - 业务
/*点击右上角按钮**/
- (void)clickRightBtn:(UIButton *)btn
{
    
    [self rightBarbtnBusinessWithQuestionProgress:self.mainModel.questionProgress];
    
}
/*审核通过**/
- (void)approved
{
    [self updateProgressWithProgress:@"1" withErrorMsg:nil success:^(id aOjec) {
        
    } fail:^(NSError *aError) {
        
    }];
}
/*受理该问题**/
- (void)acceptProblem
{
    [self updateProgressWithProgress:@"2" withErrorMsg:nil success:^(id aOjec) {
        
    } fail:^(NSError *aError) {
        
    }];
}
/*处理反馈**/
- (void)feedBack
{
    [self creatActionSheetWithArray:self.actionSheetSource];
    
}
/*政府端操作:审核不通过，关闭问题**/
- (void)closeFeedBack
{
    NFeedbackPopView *po = [[[NSBundle mainBundle]loadNibNamed:@"NFeedbackPopView" owner:self options:nil]lastObject];
    po.topTitleLabel.text = @"审核不通过原因";
    po.popBlock = ^(NSString *text) {
        //        YYLog(@"%@",text);
        [self updateProgressWithProgress:@"9" withErrorMsg:text success:^(id aOjec) {
            
        } fail:^(NSError *aError) {
            
        }];
    };
    [self.view addSubview:po];
}
///自行回复
//自行答复和添加答复内容   都是自行回复，这种情况下questionStatus=1，后天会直接返回questionContent = "【处理进度：等待用户确认】 @“这里是具体内容”"
- (void)toRecover{
    WeakStament(ws);
    NWriteMsgViewController *writeMsgVC = [NWriteMsgViewController new];
    writeMsgVC.problemId = self.mainModel.questionId;
    writeMsgVC.isAutoReplay = YES;
     writeMsgVC.officeName = self.officeName;
    [ws.navigationController pushViewController:writeMsgVC animated:YES];
}
/*留言**/
- (void)continueMessage{
    WeakStament(ws);
    NWriteMsgViewController *writeMsgVC = [NWriteMsgViewController new];
    writeMsgVC.problemId = self.mainModel.questionId;
    writeMsgVC.officeName = self.officeName;
    [ws.navigationController pushViewController:writeMsgVC animated:YES];
}
/*确认解答**/
- (void)confirmAnswer
{
    WeakStament(ws);
    NConfirmPopView *confirmView = [NConfirmPopView new];
    [confirmView show];
    confirmView.buttonClick = ^(UIButton *sender) {
        [ws gotoSorce];
    };
}

/*重新发起问题反馈**/
- (void)againFeedback
{
    WeakStament(ws);
    NCommitFBViewController *vc = [NCommitFBViewController new];
    NSString *problemType = ws.mainModel.questionType;
    NSString *privacyType = ws.mainModel.questionPrivilege;
    switch ([problemType integerValue]) {
        case 1:
            {
                vc.problemType = @"咨询";
            }
            break;
        case 2:
        {
            vc.problemType = @"投诉";
        }
            break;
        case 3:
        {
            vc.problemType = @"建议";
        }
            break;
            
        default:
            break;
    }
    switch ([privacyType integerValue]) {
        case 1:
        {
            vc.privacy = @"公开该问题";
        }
            break;
        case 2:
        {
            vc.privacy = @"仅自己可见";
        }
            break;
            
        default:
            break;
    }
    vc.ttitle = ws.mainModel.questionTitle;
    vc.content = ws.mainModel.questionContent;
    vc.unit = ws.mainModel.questionOfficeName;
    [ws.navigationController pushViewController:vc animated:YES];
}

/*确认解答后 弹框**/
- (void)gotoSorce
{
    WeakStament(ws);
    [ws updateProgressWithProgress:@"6" withErrorMsg:nil success:^(id aOjec) {
        if ([[aOjec objectForKey:@"code"] isEqualToString:@"0"]) {
            
            [ws toStarSorce];
            
        }else{
            [AFToast showText:@"更改失败"];
        }
    } fail:^(NSError *aError) {
        [AFToast showText:@"更改失败"];
    }];
    
}
/*星星评分**/
- (void)toStarSorce
{
    WeakStament(ws);
    if (self.hasShowStar) {
        return;
    }
    NStarPopView *tarview = [NStarPopView new];
    [tarview show];
    self.hasShowStar = YES;
    tarview.buttonClick = ^(UIButton * sender,NSDictionary *starDict) {
        NSDictionary *dict=[[NSDictionary alloc]initWithObjectsAndKeys:ws.mainModel.questionId,@"degreeQuestionId",ws.mainModel.questionOfficeId,@"degreeServiceId",[NUserModel shareInstance].userInfoId, @"degreeUserId",[starDict objectForKey:@"101"],@"degreeMain",[starDict objectForKey:@"201"],@"degreeAnswer",[starDict objectForKey:@"301"],@"degreeWork",[starDict objectForKey:@"401"],@"degreeOffice", nil];
        [[NNRCNetRequest manager]postWithUrl:kDegreeAdd params:(id)dict success:^(id result) {
            if ([[result objectForKey:@"code"] isEqualToString:@"0"]) {
                [AFToast showText:@"评价成功"];
                //更改状态为 已评价
                [ws updateProgressWithProgress:@"7" withErrorMsg:nil success:^(id aOjec) {
                    
                } fail:^(NSError *aError) {
                    
                }];
            }else{
                [AFToast showText:@"评价失败"];
            }
        } failure:^(id failure) {
             [AFToast showText:@"评价失败"];
        }];
    };
}

-(NStarPopView *)popView{
    if (!_popView) {
        _popView = [NStarPopView new];
    }
    return _popView;
}
#pragma mark -问题进度：//0待审核,1等待受理，2已受理，3已转交相关部门，4自行解答，5等待用户确认,6等待用户评价,7已评价,8已关闭,9审核未通过,10删除

#pragma mark - /*更改问题进度**/
- (void)updateProgressWithProgress:(NSString *)progress withErrorMsg:(NSString *)msg success:(void (^)(id aOjec))aSuccess fail:(void (^)(NSError *aError))aFail
{
    // progress 说明
    // 问题进度：0待审核,1等待受理，2已受理，3已转交相关部门，4自行解答，5等待用户确认,6等待用户评价,7已评价,8已关闭,9审核未通过,10删除
    [self updateProgressWithProgress:progress withOfficeID:nil withErrorMsg:msg success:aSuccess fail:aFail];
}
- (void)updateProgressWithProgress:(NSString *)progress withOfficeID:(NSString *)officeID withErrorMsg:(NSString *)msg success:(void (^)(id aOjec))aSuccess fail:(void (^)(NSError *aError))aFail
{
    // progress 说明
    // 问题进度：0待审核,1等待受理，2已受理，3已转交相关部门，4自行解答，5等待用户确认,6等待用户评价,7已评价,8已关闭,9审核未通过,10删除
    WeakStament(ws);
    
    if (!msg) {
        msg=@"";
    }
    if (!officeID) {
        officeID=@"";
    }
    NSDictionary *dict=[[NSDictionary alloc]initWithObjectsAndKeys:progress,@"questionProgress",ws.mainModel.questionId,@"questionId",[NUserModel shareInstance].userInfoId, @"answerQuestionUserId",msg,@"questionErrorMsg",officeID,@"questionOfficeId", nil];
    
    [[NNRCNetRequest manager]postWithUrl:kProblemUpdate params:dict success:^(id result) {
        if ([[result objectForKey:@"code"] isEqualToString:@"0"]) {
            [ws loadDetialData];
            aSuccess(result);
//            [AFToast showText:@"更改成功"];
        }else{
            [AFToast showText:@"更改失败"];
        }
    } failure:^(id failure) {
        aFail(failure);
        [AFToast showText:@"更改失败"];
    }];

}
#pragma mark - sheet  & sheet Delegate
- (void)creatActionSheetWithArray:(NSArray *)array
{
    ZFActionSheet *sheet = [ZFActionSheet actionSheetWithTitle:@"请选择" confirms:array cancel:@"取消" style:ZFActionSheetStyleDefault];
    sheet.delegate = self;
    [sheet show];
}
- (void)clickAction:(ZFActionSheet *)actionSheet atIndex:(NSUInteger)index
{
    NSString *str = [self.actionSheetSource objectAtIndex:index];
    if ([str isEqualToString:ActionSheetTextClose]) {
        //关闭该反馈
        [self updateProgressWithProgress:@"8" withErrorMsg:nil success:^(id aOjec) {
            [AFToast showText:@"问题已关闭"];
        } fail:^(NSError *aError) {
            
        }];
    }else if ([str isEqualToString:ActionSheetTextDelete]){
        //删除该反馈
        [self updateProgressWithProgress:@"10" withErrorMsg:nil success:^(id aOjec) {
             [AFToast showText:@"删除成功"];
        } fail:^(NSError *aError) {
            
        }];
    }else if ([str isEqualToString:ActionSheetTextTransfer]){
        //转交相关部门
        [self loadUnitDataWithsuccess:^(id aOjec) {
            STPickerSingle *single = [[STPickerSingle alloc]init];
            single.tag = 333;
            [single setArrayData:aOjec];
            [single setTitle:@"科室选择"];
            [single setTitleUnit:@""];
            [single setDelegate:self];
            [single show];
        } fail:^(NSError *aError) {
            
        }];
        
    }else if ([str isEqualToString:ActionSheetTextReply]){
        //自行答复该问题
        [self toRecover];
    }
}
/*请求科室数据**/
- (void)loadUnitDataWithsuccess:(void (^)(id aOjec))aSuccess fail:(void (^)(NSError *aError))aFail {
    WeakStament(ws);
    [[NNRCNetRequest manager]getWithUrl:kKeshiRoomUrl params:nil success:^(id result) {
        if ([result[@"code"] isEqualToString:@"0"]) {
            NSArray *array = result[@"page"];
            self.departmentSource = [NSArray arrayWithArray:array];
            aSuccess([ws formData:array]);
        }

    } failure:^(id failure) {
        aFail(failure);
    }];
}
/*格式化相关部门数据**/
- (NSArray *)formData:(NSArray *)data
{
    NSMutableArray *array = [NSMutableArray array];
    for (NSDictionary *dict in data) {
        NUnitRoomModel *model = [NUnitRoomModel mj_objectWithKeyValues:dict];
        NSString *name = model.officeName;
        [array addObject:name];
    }
    return array;
}
#pragma mark - STPickerSingleDelegate
- (void)pickerSingle:(STPickerSingle *)pickerSingle selectedTitle:(NSString *)selectedTitle selectIndex:(NSInteger)selectIndex
{
    NUnitRoomModel *model = [NUnitRoomModel mj_objectWithKeyValues:[self.departmentSource objectAtIndex:selectIndex]];
    
    [self updateProgressWithProgress:@"3" withOfficeID:[NSString stringWithFormat:@"%ld",model.officeId] withErrorMsg:nil success:^(id aOjec) {
        if ([[aOjec objectForKey:@"code"] isEqualToString:@"0"]) {
            [AFToast showText:@"转交成功"];
//            [SVProgressHUD dismiss];
        }else{
            [AFToast showText:@"转交失败"];
//             [SVProgressHUD dismiss];
        }
    } fail:^(NSError *aError) {
        [AFToast showText:@"转交失败"];
//         [SVProgressHUD dismiss];
    }];
    
}

-(void)backItemClick{
    [super backItemClick];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"jjjj" object:nil];
}

@end
